package com.offcn.controller;

import com.offcn.entity.Product;
import com.offcn.entity.Trolley;
import com.offcn.entity.User;
import com.offcn.service.CardService;
import com.offcn.service.ProductService;
import com.offcn.utils.Car;
import com.offcn.utils.CarItems;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/car")
public class CarController {

    @Resource
    private ProductService productService;
    @Resource
    private CardService cardService;
   /* @Resource
    private IUserAddressService userAddressService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IOrderDetailService orderDetailService;*/


    //改前
//    @RequestMapping("/addOne")
//    @ResponseBody
//    public String addOne(@RequestParam(defaultValue="0")int id, @RequestParam(defaultValue="0")int quantity, HttpSession session) {
//       User user = new User();
//       user.setUid(4);
//      int num =    cardService.addTolley(id,4);
//         cardService.selectAll(4);
//
//      return num+"";
//
//    }
    //商品详情页购物车加1
    @RequestMapping("/addOne")
    @ResponseBody
    public String addOne(@RequestParam(defaultValue = "0") int id, HttpSession session) {
        int num = cardService.addTrolley(id, ((User) session.getAttribute("user")).getUid());

        return num + "";
    }
    //购物车中商品数量加1
    @RequestMapping("/plusOne")
    public  String plusOne(@RequestParam(defaultValue = "0") int id, HttpSession session){
        int  num  = cardService.addTrolley(id, ((User) session.getAttribute("user")).getUid());
        return  "redirect:/car/addMyCard";
    }
    //购物车中商品数量减1
    @RequestMapping("/reduceOne")
    public  String reduceOne(@RequestParam(defaultValue = "0") int id, HttpSession session){
           int  num  = cardService.reduceTrolley(id, ((User) session.getAttribute("user")).getUid());
           return  "redirect:/car/addMyCard";
    }

    @RequestMapping(value = "/addMyCard")
    public String addMyCard(Model model, HttpSession session) {
        List<Trolley> trolleys = cardService.selectByUid(((User) session.getAttribute("user")).getUid());
        model.addAttribute("trolleys", trolleys);
        return "mycart";
    }


    @RequestMapping(value = "/deleteTid")
    public String deleteTid(@RequestParam(defaultValue = "0") int id, HttpSession session){
        int num=cardService.deleteByUidAndId(id, ((User) session.getAttribute("user")).getUid());
        return  "redirect:/car/addMyCard";
    }

}
